<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>copyToClipboardAsync</title>
</head>

<body>
    <div id="test">被复制的文本</div>
    <button type="button" id="copyBtn">点我复制</button>
    <script>
        const copyToClipboard = str => {
            if (navigator && navigator.clipboard && navigator.clipboard.writeText) {
                return navigator.clipboard.writeText(str);
            }
            return Promise.reject('The Clipboard API is not available.');
        };
        const btn = document.getElementById('copyBtn');
        const text = document.getElementById('test');
        btn.onclick = function () {
            copyToClipboard(text.textContent);
            alert('复制成功');
        }
    </script>
</body>

</html>